import 'package:badges/badges.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:im_module/common/message_cache_key.dart';
import 'package:im_module/common/time_message.dart';
import 'package:im_module/message/ImChatWidget.dart';
import 'package:im_module/message/ImSystemMessage.dart';
import 'package:tencent_im_sdk_plugin/enum/message_elem_type.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_conversation.dart';

import 'ImHeadImageWidget.dart';

///会话列表
class ConversationItem extends StatelessWidget {
  final V2TimConversation _data;

  ConversationItem(this._data);

  TextStyle mTextStyle = TextStyle(fontSize: 13, color: Color(0xff999999));

  @override
  Widget build(BuildContext context) {
    //print("会话信息:${_data.toJson()}");
    return ListTile(
      leading: Badge(
          showBadge: _data.unreadCount! > 0 ? true : false,
          shape: BadgeShape.circle,
          badgeColor: Colors.red,
          toAnimate: false,
          borderRadius: BorderRadius.circular(100),
          alignment: Alignment.topRight,
          badgeContent: Text('${_data.unreadCount}',
              style: TextStyle(color: Colors.white, fontSize: 12)),
          child: ImHeadImageWidget("${_data.faceUrl!}")),
      title: Text(
        "${_data.showName}",
        style: TextStyle(fontSize: 16, color: Color(0xff333333)),
      ),
      subtitle: Padding(
        padding: const EdgeInsets.symmetric(vertical: 4),
        child: LayoutBuilder(
          builder: (BuildContext context, BoxConstraints constraints) {
            switch (_data.lastMessage?.elemType) {
              case MessageElemType.V2TIM_ELEM_TYPE_TEXT:
                return Text(
                  "${_data.lastMessage?.textElem?.text}",
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: mTextStyle,
                );
              case MessageElemType.V2TIM_ELEM_TYPE_IMAGE:
                return Text(
                  "[图片]",
                  style: mTextStyle,
                );
              case MessageElemType.V2TIM_ELEM_TYPE_LOCATION:
                return Text(
                  "[地理位置]",
                  style: mTextStyle,
                );
              case MessageElemType.V2TIM_ELEM_TYPE_CUSTOM:
                return Text(
                  "[自定义消息]",
                  style: mTextStyle,
                );
              case MessageElemType.V2TIM_ELEM_TYPE_GROUP_TIPS:
                return Text(
                  "[群消息]",
                  style: mTextStyle,
                );
              default:
                return Text(
                  "[其他消息]",
                  style: mTextStyle,
                );
            }
          },
        ),
      ),
      trailing: Text(
        "${MessageTimeUtils.format(_data.lastMessage!.timestamp!)}",
        style: TextStyle(fontSize: 12, color: Color(0xff999999)),
      ),
      onTap: () {
        String id;
        //1是单聊 2是群聊
        if (_data.type == 1) {
          id = _data.userID!;
        } else {
          id = _data.groupID!;
        }

        Navigator.push(context, MaterialPageRoute(builder: (_) {
          //如果用户id=3是系统消息的发送者 进入系统消息
          if (id == MessageCacheKey.SYSTEM_MESSAGE_USER_ID) {
            return new ImSystemMessage(id);
          } else {
            return new ImChatWidget(
              type: _data.type!,
              id: id,
              niceName: _data.showName, //对方好友备注->对方昵称->对方的 userID
            );
          }
        }));
      },
    );
  }
}
